Skip to content

Add into_text_value() to PrimitiveValues#718

Open
techtenk wants to merge 4 commits intoEnet4:masterfrom
techtenk:techtenk/into_text_value
Open

Add into_text_value() to PrimitiveValues#718
techtenk wants to merge 4 commits intoEnet4:masterfrom
techtenk:techtenk/into_text_value

Conversation

@techtenk
Copy link
Copy Markdown

@techtenk techtenk commented Dec 19, 2025

Implements: ISSUE-88

This adds into_text_value() for PrimitiveValue as well as an impl From<Cow<'_, str>> for PrimitiveValue per the suggestion in the issue above.

Assumptions to check:

  • All variants should be converted to PrimitiveValue::Str (including Empty and Strs)
  • use to_raw_str() instead of to_str() to preserve the most information (no trimming of extra spaces, etc)
  • both into_text_value() and impl From<Cow<'_, str>> for PrimitiveValue are useful for convenience/completeness, even though into_text_value() is just PrimitiveValue::from(self.to_raw_str())

Other things to check:

  • I didn't write test cases for all variants, figuring it become redundant with the to_raw_str() tests. Let me know if I should.
  • double check the correctness of the documentation. I tried to follow the pattern but I'm new to the project and might have mis-typed

@techtenk techtenk changed the title Techtenk/into text value Add into_text_value() to PrimitiveValues Dec 19, 2025
@techtenk techtenk force-pushed the techtenk/into_text_value branch from d0e4053 to f3af414 Compare December 19, 2025 20:40
@Enet4 Enet4 added enhancement A-lib Area: library C-core Crate: dicom-core labels Dec 22, 2025
@Enet4 Enet4 self-requested a review December 22, 2025 14:41
Copy link
Copy Markdown
Owner

@Enet4 Enet4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your pull request. Please let me know if you are available to reiterate on it per the comments below.

The only major concern that makes me put this on hold is the fact that it can easily create invalid date-time values.

///
/// assert_eq!(
/// text_value,
/// PrimitiveValue::Str("2024-12-25".to_string())
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shows... an interesting caveat. Dates are only DICOM-encoded correctly when converted via to_encoded, but to_raw_str does not use it (which would be fine because to_str and to_raw_str are ill-advised for DICOM serialization anyway, so they are not expected to be pushed back into a DICOM value).

The current behavior of this method will come across as surprising, so we will have to adjust it accordingly.

  • Date and date-time values in PrimitiveValue::Date and PrimitiveValue::DateTime are encoded to their standard DICOM textual form.
  • Other binary variants are converted to strings via to_string.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can work on this. For clarification, is it as simple as calling to_encoded() when appropriate and updating the docs? Or am I missing some nuance?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So yes, for the variants Da and Dt they should be converted to text via to_encoded(). The rule for concatenating multiple values into a single string would be the same (joined together with backslash as the separator).

techtenk and others added 2 commits January 12, 2026 17:36
Co-authored-by: Eduardo Pinho <enet4mikeenet@gmail.com>
Co-authored-by: Eduardo Pinho <enet4mikeenet@gmail.com>
Copy link
Copy Markdown
Owner

@Enet4 Enet4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will signal this PR as needing changes before proceeding. Please let me know if you would prefer someone else to take over.

.unwrap()]);
assert_eq!(
value.into_text_value(),
PrimitiveValue::Str("2012-12-21 09:30:01".to_string())
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in past comments, dates and times (DA, TM, DT) should continue to comply with their value representation after being converted to text, as per PS3.5 section 6.2. This test, the implementation, and the documentation of the method need to be updated accordingly.

Suggested change
PrimitiveValue::Str("2012-12-21 09:30:01".to_string())
PrimitiveValue::Str("20121221093001".to_string())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-lib Area: library C-core Crate: dicom-core enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants